#include "filelocalclass.h"
#include <QDebug>
#include <QThread>
FileLocalClass::FileLocalClass(QObject *parent) : QObject(parent)
{
}

void FileLocalClass::initFileLocal(const QString &useroc)
{
    this->useroc=useroc;
}



//从文件夹中获取头像信息，文件名即OC号
void FileLocalClass::startLoadAvatar()
{
    QHash<QString,QPixmap> result;
    QString path="./User/"+useroc+"/Avatar";
    QDir dir(path);
    QStringList filelist=dir.entryList();

    for(QString name:filelist)
    {
        QString curfilepath=QString(path+"/"+name);
        QFileInfo info(curfilepath);
        if(info.isFile())
        {
            QString name=info.fileName().split(".",QString::SkipEmptyParts).at(0);
            result.insert(name,curfilepath);
        }
    }
    emit s_loadAvatarDone(result);

}

//将发送的文件保存到./File
void FileLocalClass::addSendFile(QPair<QString,QString> item)
{
    this->list.append(item);
}

void FileLocalClass::saveFrontOne()
{
    copyFile(list.front());
    list.pop_front();
}


//复制文件
//文件绝对路径    ./File/2024-05/[2]myimg.png
bool FileLocalClass::copyFile(const QPair<QString,QString> item)
{
    const QString sourceFilePath=item.first;
    const QString destinationFilePath=item.second;
    QFile sourceFile(sourceFilePath);
    if (!sourceFile.exists()) {
        qDebug() << "Source file does not exist:" << sourceFilePath;
        return false;
    }

    if (!sourceFile.open(QIODevice::ReadOnly)) {
        qDebug() << "Failed to open source file:" << sourceFilePath;
        return false;
    }
    QDir desdir(destinationFilePath);
    desdir.cdUp();
    if(!desdir.exists())
    {
        desdir.mkpath(desdir.absolutePath());
    }
    QFile destinationFile(destinationFilePath);


    if (!destinationFile.open(QIODevice::WriteOnly)) {
        qDebug() << "Failed to open destination file for writing:" << destinationFilePath;
        sourceFile.close(); // 不要忘记关闭源文件
        return false;
    }

    QByteArray array=sourceFile.readAll();
    destinationFile.write(array);

    sourceFile.close();
    destinationFile.close();
    return true;
}

void FileLocalClass::deleteFile(QString filepath)
{
    if(QFile::remove(filepath))
        qDebug()<<"delete file success";
}

void FileLocalClass::setUserAvatar(QString path)
{
    QFileInfo info(path);
    copyFile(QPair<QString,QString>(path,"./User/"+useroc+"/Avatar/"+"user.png"));
}

void FileLocalClass::initSettings()
{
    SettingParser::getInstance()->initParser("./OneChat.occ");
}

void FileLocalClass::setValue(QString key, QString val)
{
    SettingParser::getInstance()->setValue(key,val);
}

void FileLocalClass::refreshUser()
{
    AvatarLib::getInstance()->refreshUser(useroc);
}

void FileLocalClass::clearAvatar()
{
    AvatarLib::getInstance()->clearAvatar();
}

void FileLocalClass::updateUserOC(QString newuseroc)
{
    qDebug()<<"./User/"+useroc<<"./User/"+newuseroc;
    if(QDir().rename("./User/"+useroc,"./User/"+newuseroc))
        useroc=newuseroc;
    else
        qDebug()<<"rename userfile failed";
}

void FileLocalClass::createNewUserAvatar()
{
    QString parentpath=QString("./User/"+useroc+"/Avatar");
    QDir().mkpath(parentpath);

}

void FileLocalClass::moveAvatarCacheToAvatar(const QString &friendoc)
{
    QString sourcepath="./User/"+useroc+"/Cache/"+friendoc+".png";
    QString despath="./User/"+useroc+"/Avatar/"+friendoc+".png";
    copyFile(QPair<QString,QString>(sourcepath,despath));
}

void FileLocalClass::appendAvatar(const QString &oc, const QString &friendoc)
{
    AvatarLib::getInstance()->appendAvatar(oc,friendoc);
}

void FileLocalClass::deleteAvatar(const QString &oc)
{
    QFile::remove("./User/"+useroc+"/Avatar/"+oc+".png");
}

void FileLocalClass::flushSettings()
{
    SettingParser::getInstance()->flushBuf();
}

